# 定义内容审核页面
from selenium.webdriver.common.by import By

from base.mis.base import BasePage, BaseHandle
from until import choice_channel, is_exist, UtilsDriver


# 定义对象库层
class AuditPage(BasePage):
    def __init__(self):
        super().__init__()  # 这里必须要初始化因为在BasePage里有初始化
        # 文章标题
        self.title = By.XPATH, '//h1/text()'
        # 状态选择
        self.channel = By.XPATH, '//*[@id="channel"]/text()'
        # 查询按钮
        self.qyery_btn = By.XPATH, '//*[@id="qyrry"]/text()'
        # 通过按钮
        self.pass_btn = By.XPATH, '//*[@id="pass"]/text()'
        # 确定按钮
        self.confirm_btn = By.XPATH, '//*[@id="confirm"]/text()'
        # 结束时间框
        self.time_end = By.XPATH, '//*[@id="submit"]/text()'

    # 定位标题输入框
    def find_title(self):
        return self.get_element(self.title)

    # 定位状态选择框
    def find_channel(self):
        return self.get_element(self.channel)

    # 定位查询按钮
    def find_qyery(self):
        return self.get_element(self.qyery_btn)

    # 定位通过按钮
    def find_pass(self):
        return self.get_element(self.pass_btn)

    # 定位确定按钮
    def find_confirm(self):
        return self.get_element(self.confirm_btn)

    # 定位结束时间框
    def find_time(self):
        return self.get_element(self.time_end)


# 定义操作层对象
class AuditHandle(BaseHandle):
    def __init__(self):
        self.audit_page = AuditPage()

    # 输入文章标题
    def input_title(self, title):
        self.input_text(self.audit_page.find_title(), title)

    # 选择状态
    def choice_channel(self, channel):
        choice_channel(self.audit_page.driver, self.audit_page.find_channel(), channel)

    def input_time_end(self, time):
        self.input_text(self.audit_page.time_end, time)

    # 点击查询按钮
    def click_qyery(self):
        self.audit_page.find_qyery().click()

    # 点击通过按钮
    def click_pass(self):
        self.audit_page.find_pass().click()

    # 点击确定按钮
    def click_confirm(self):
        self.audit_page.find_confirm().click()


# 定义业务层
class AuditProxy:
    def __init__(self):
        self.audit_handle = AuditHandle()

    # 审核业务
    def audit_article(self, title, channel, time):
        self.audit_handle.input_title(title)  # 输入文章标题
        time.sleep(1)
        self.audit_handle.choice_channel(channel)  # 选择状态
        time.sleep(1)
        self.audit_handle.input_time_end(time)
        time.sleep(1)
        self.audit_handle.click_qyery()  # 点击查询
        time.sleep(1)
        self.audit_handle.click_pass()  # 点击通过
        time.sleep(1)
        self.audit_handle.click_confirm()  # 点击确认
        time.sleep(1)

    # 审核查询通过（断言审核业务是否成功）
    def audit_article_pass(self, title):
        self.audit_handle.input_title(title)
        self.audit_handle.choice_channel("审核通过")
        self.audit_handle.click_qyery()
        return is_exist(UtilsDriver.get_mis_driver(), title)
